package org.pay.shop.demo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.pay.core.common.util.PayDigestUtil;
import org.pay.core.common.util.PayUtil;

import java.util.Map;

/**
 * Created by yangshancheng on 16/5/5.
 */
public class AccountDemo {

    // 商户ID
    static final String mchId = "20000027";//20001223,20001245
    static final String appId = "2e6c0342d38148db970585484b693b4e";
    static final String passageId = "1003";
    // 私钥
    static final String key = "NklfnwSUjJrnHkAsS9wCabrAZ6sGDz8EgHQtTfVPtePUugiI";

    //static final String baseUrl = "http://pay.t.pay.org/api";
    static final String baseUrl = "http://localhost:3020/api";
    //static final String notifyUrl = "http://www.baidu.com"; // 本地环境测试,可到ngrok.cc网站注册
    static final String notifyUrl = "http://shop.pay.org/goods/payNotify";

    public static void main(String[] args) {
        //openAccountTest();
        //recharge("1523868599758", 900l);
        //queryAccountTest("1523868599758");
        //debit("1523868599758", 50l);
        trans("1523868599758", "1522846820155", 50l);
        //changeUseableBalance("1523868599758", (short) 1, 1000l);  // 1 增加 -1 减少
        //updateAmount("1522847195332", (short) -1, 1l, (short) 0, 1l);
    }

    // 开户
    static String openAccountTest() {
        String userId = System.currentTimeMillis()+"";
        JSONObject paramMap = new JSONObject();
        paramMap.put("mchId", mchId);                           // 商户ID
        paramMap.put("userId", userId);                         // 用户ID
        String reqSign = PayDigestUtil.getSign(paramMap, key);
        paramMap.put("sign", reqSign);                              // 签名
        String reqData = "params=" + paramMap.toJSONString();
        System.out.println("请求支付中心开户接口,请求数据:" + reqData);
        String url = baseUrl + "/account/open?";
        String result = PayUtil.call4Post(url + reqData);
        System.out.println("请求支付中心开户接口,响应数据:" + result);
        Map retMap = JSON.parseObject(result);
        if("SUCCESS".equals(retMap.get("retCode"))/* && "SUCCESS".equalsIgnoreCase(retMap.get("resCode").toString())*/) {
            // 验签
            String checkSign = PayDigestUtil.getSign(retMap, key, "sign");
            String retSign = (String) retMap.get("sign");
            if(checkSign.equals(retSign)) {
                System.out.println("=========支付中心开户验签成功=========");
            }else {
                System.err.println("=========支付中心开户验签失败=========");
                return null;
            }
        }
        return retMap.get("userId")+"";
    }

    // 查询
    static String queryAccountTest(String userId) {
        JSONObject paramMap = new JSONObject();
        paramMap.put("mchId", mchId);                           // 商户ID
        paramMap.put("userId", userId);                         // 用户ID
        String reqSign = PayDigestUtil.getSign(paramMap, key);
        paramMap.put("sign", reqSign);                              // 签名
        String reqData = "params=" + paramMap.toJSONString();
        System.out.println("请求支付中心查询账户接口,请求数据:" + reqData);
        String url = baseUrl + "/account/query?";
        String result = PayUtil.call4Post(url + reqData);
        System.out.println("请求支付中心查询账户接口,响应数据:" + result);
        Map retMap = JSON.parseObject(result);
        if("SUCCESS".equals(retMap.get("retCode"))) {
            // 验签
            String checkSign = PayDigestUtil.getSign(retMap, key, "sign");
            String retSign = (String) retMap.get("sign");
            if(checkSign.equals(retSign)) {
                System.out.println("=========支付中心开户验签成功=========");
            }else {
                System.err.println("=========支付中心开户验签失败=========");
                return null;
            }
        }
        return retMap.get("userId")+"";
    }

    // 充值
    static String recharge(String userId, Long amount) {
        JSONObject paramMap = new JSONObject();
        paramMap.put("mchId", mchId);                           // 商户ID
        paramMap.put("userId", userId);                         // 用户ID
        paramMap.put("amount", amount);                         // 充值金额

        String reqSign = PayDigestUtil.getSign(paramMap, key);
        paramMap.put("sign", reqSign);                              // 签名
        String reqData = "params=" + paramMap.toJSONString();
        System.out.println("请求支付中心账户充值接口,请求数据:" + reqData);
        String url = baseUrl + "/account/recharge?";
        String result = PayUtil.call4Post(url + reqData);
        System.out.println("请求支付中心账户充值接口,响应数据:" + result);
        Map retMap = JSON.parseObject(result);
        if("SUCCESS".equals(retMap.get("retCode"))) {
            // 验签
            String checkSign = PayDigestUtil.getSign(retMap, key, "sign");
            String retSign = (String) retMap.get("sign");
            if(checkSign.equals(retSign)) {
                System.out.println("=========支付中心账户充值验签成功=========");
            }else {
                System.err.println("=========支付中心账户充值验签失败=========");
                return null;
            }
        }
        return retMap.get("userId")+"";
    }

    // 扣款
    static String debit(String userId, Long amount) {
        JSONObject paramMap = new JSONObject();
        paramMap.put("mchId", mchId);                           // 商户ID
        paramMap.put("userId", userId);                         // 用户ID
        paramMap.put("amount", amount);                         // 充值金额

        String reqSign = PayDigestUtil.getSign(paramMap, key);
        paramMap.put("sign", reqSign);                              // 签名
        String reqData = "params=" + paramMap.toJSONString();
        System.out.println("请求支付中心账户扣款接口,请求数据:" + reqData);
        String url = baseUrl + "/account/debit?";
        String result = PayUtil.call4Post(url + reqData);
        System.out.println("请求支付中心账户扣款接口,响应数据:" + result);
        Map retMap = JSON.parseObject(result);
        if("SUCCESS".equals(retMap.get("retCode"))) {
            // 验签
            String checkSign = PayDigestUtil.getSign(retMap, key, "sign");
            String retSign = (String) retMap.get("sign");
            if(checkSign.equals(retSign)) {
                System.out.println("=========支付中心账户扣款验签成功=========");
            }else {
                System.err.println("=========支付中心账户扣款验签失败=========");
                return null;
            }
        }
        return retMap.get("userId")+"";
    }

    // 转账
    static String trans(String fromUserId, String toUserId, Long amount) {
        JSONObject paramMap = new JSONObject();
        paramMap.put("mchId", mchId);                           // 商户ID
        paramMap.put("fromUserId", fromUserId);                 // 转出用户ID
        paramMap.put("toUserId", toUserId);                     // 转入用户ID
        paramMap.put("amount", amount);                         // 充值金额

        String reqSign = PayDigestUtil.getSign(paramMap, key);
        paramMap.put("sign", reqSign);                              // 签名
        String reqData = "params=" + paramMap.toJSONString();
        System.out.println("请求支付中心账户转账接口,请求数据:" + reqData);
        String url = baseUrl + "/account/transfer?";
        String result = PayUtil.call4Post(url + reqData);
        System.out.println("请求支付中心账户转账接口,响应数据:" + result);
        Map retMap = JSON.parseObject(result);
        if("SUCCESS".equals(retMap.get("retCode"))) {
            // 验签
            String checkSign = PayDigestUtil.getSign(retMap, key, "sign");
            String retSign = (String) retMap.get("sign");
            if(checkSign.equals(retSign)) {
                System.out.println("=========支付中心账户转账验签成功=========");
            }else {
                System.err.println("=========支付中心账户转账验签失败=========");
                return null;
            }
        }
        return retMap.get("userId")+"";
    }

    // 变更可用余额
    static String changeUseableBalance(String userId, Short changeType, Long amount) {
        JSONObject paramMap = new JSONObject();
        paramMap.put("mchId", mchId);                           // 商户ID
        paramMap.put("userId", userId);                         // 用户ID
        paramMap.put("changeType", changeType);                 // 变更类型: 1 增加, -1 减少
        paramMap.put("amount", amount);                         // 充值金额

        String reqSign = PayDigestUtil.getSign(paramMap, key);
        paramMap.put("sign", reqSign);                              // 签名
        String reqData = "params=" + paramMap.toJSONString();
        System.out.println("请求支付中心账户可用余额变更接口,请求数据:" + reqData);
        String url = baseUrl + "/account/change_useable_balance?";
        String result = PayUtil.call4Post(url + reqData);
        System.out.println("请求支付中心账户可用余额变更接口,响应数据:" + result);
        Map retMap = JSON.parseObject(result);
        if("SUCCESS".equals(retMap.get("retCode"))) {
            // 验签
            String checkSign = PayDigestUtil.getSign(retMap, key, "sign");
            String retSign = (String) retMap.get("sign");
            if(checkSign.equals(retSign)) {
                System.out.println("=========支付中心账户可用余额变更验签成功=========");
            }else {
                System.err.println("=========支付中心账户可用余额变更验签失败=========");
                return null;
            }
        }
        return retMap.get("userId")+"";
    }

    // 修改账户金额
    static String updateAmount(String userId, Short changeType, Long amount, Short useableChangeType, Long useableAmount) {
        JSONObject paramMap = new JSONObject();
        paramMap.put("mchId", mchId);                           // 商户ID
        paramMap.put("userId", userId);                         // 用户ID
        paramMap.put("changeType", changeType);                 // 变更类型: 1 增加, -1 减少, 0 不变
        paramMap.put("amount", amount);                         // 账户余额
        paramMap.put("useableChangeType", useableChangeType);   // 可用余额变更类型:  1 增加, -1 减少, 0 不变
        paramMap.put("useableAmount", useableAmount);           // 可用余额

        String reqSign = PayDigestUtil.getSign(paramMap, key);
        paramMap.put("sign", reqSign);                              // 签名
        String reqData = "params=" + paramMap.toJSONString();
        System.out.println("请求支付中心账户金额修改接口,请求数据:" + reqData);
        String url = baseUrl + "/account/update_amount?";
        String result = PayUtil.call4Post(url + reqData);
        System.out.println("请求支付中心账户金额修改接口,响应数据:" + result);
        Map retMap = JSON.parseObject(result);
        if("SUCCESS".equals(retMap.get("retCode"))) {
            // 验签
            String checkSign = PayDigestUtil.getSign(retMap, key, "sign");
            String retSign = (String) retMap.get("sign");
            if(checkSign.equals(retSign)) {
                System.out.println("=========支付中心账户金额修改验签成功=========");
            }else {
                System.err.println("=========支付中心账户金额修改验签失败=========");
                return null;
            }
        }
        return retMap.get("userId")+"";
    }

}
